detailUrl = "http://localhost:8080/book/detail/";
 res = null;
var totalPage;

$(document).ready(function () {
    currentPage('1');
    $('#box').paging({
        initPageNo: 1, // 初始页码
        totalPages: totalPage, //总页数
        // totalCount: '合计' + setTotalCount + '条数据', // 条目总数
        slideSpeed: 600, // 缓动速度。单位毫秒
        jump: true, //是否支持跳转
        callback: function(page) { // 回调函数
            currentPage(page);
        },
    });
    getRateBooks();
    var username = getCookie('username');
    console.log(username);
    if (username != "null" && username != null) {
        $('#userName').text('你好'+username);
        $('#login').hide();
    } else
        $('#logout').hide()


});

function logout() {
    $.cookie('userId', null);
    $.cookie('username', null);
    location.reload();
}


function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

function currentPage(page){
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/pageList?page="+page,
        data: "",
        async: false,
        success:function (data) {
            $('.left_main').empty();
            console.log(data);
            console.log(data.books);
            totalPage = Math.ceil(data.bookCount  / 6);
            console.log('totalPage'+totalPage);
            // totalPage = (data.bookCount + 6 - 1) / 6;
            res = data;
            for(each in data.books)
            {
                var bookItem = '<div class="new_prod_box"><a href="details.html?bookId=ID" style="width: 150px ;height: 24px">NAME</a><div class="new_prod_bg"> <a href="details.html?bookId=ID"><img src="COVER" alt="" title="" class="thumb1" border="0" /></a></div> </div>';
                bookItem = bookItem.replace('NAME', data.books[each]['bookName']).replace(/ID/g, data.books[each]['bookId']).replace('COVER', data.books[each]['bookCover']);
                console.log(bookItem);
                $('.left_main').append(bookItem);
            }
        }
    });
}


function getRateBooks(){
    $.ajax({
        type: "get",
        url: "http://120.78.183.13/brs/book/randBooks",
        data: "",
        async: false,
        success:function (data) {
            $('.rand_books').empty();
            // totalPage = (data.bookCount + 6 - 1) / 6;
            res = data;
            var count = 0;
            for(each in data)
            {
                if (count === 3) {
                    break;
                }
                //    <div class="new_prod_box">
                //                             <a href="details.html">product name</a>
                //                             <div class="new_prod_bg">
                //                             <a href="details.html"><img src="images/thumb1.gif" alt="" title="" class="thumb" border="0" /></a>
                //                             </div>
                //                         </div>
                var bookItem = '<div class="new_prod_box">\n' +
                    '                        <a href="details.html?bookId=BOOKID">BOOKNAME</a>\n' +
                    '                        <div class="new_prod_bg">\n' +
                    '                            <span class="new_icon"><img src="images/promo_icon.gif" alt="" title=""/></span>\n' +
                    '                            <a href="details.html?bookId=BOOKID"><img src="COVER" alt="" title="" class="thumb"\n' +
                    '                                                        border="0"/></a>\n' +
                    '                        </div>\n' +
                    '                    </div>';
                bookItem = bookItem.replace('BOOKNAME', data[each]['bookName']).replace(/BOOKID/g, data[each]['bookId']).replace('COVER', data[each]['bookCover']);
                $('.rand_books').append(bookItem);
                count++;
            }
        }
    });
}


